package com.ruoyi.dynamic.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.constant.UserBalanceConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.CollectionUtils;
import com.ruoyi.dynamic.model.constant.DynamicConstant;
import com.ruoyi.dynamic.model.pojo.entity.Gift;
import com.ruoyi.dynamic.model.pojo.entity.Room;
import com.ruoyi.dynamic.model.pojo.entity.RoomAuction;
import com.ruoyi.dynamic.model.pojo.request.app.AppGiftGiveRequest;
import com.ruoyi.dynamic.model.pojo.result.app.AppUserInfoResult;
import com.ruoyi.dynamic.service.*;
import com.ruoyi.system.api.RemoteAppUserService;
import com.ruoyi.system.api.model.request.AppUserBalanceAocRequest;
import com.ruoyi.system.api.model.request.AppUserBalanceBatchOpetRequest;
import com.ruoyi.system.api.model.request.AppUserBalanceOpetRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class PayServiceImpl implements IPayService {

    @Autowired
    private IRoomGiftRecordService iRoomGiftRecordService;
    @Autowired
    private IGiftService iGiftService;
    @Autowired
    private IRoomService iRoomService;
    @Autowired
    private IParamService iParamService;
    @Autowired
    private IMessageService iMessageService;
    @Autowired
    private RemoteAppUserService remoteAppUserService;
    @Autowired
    private IRoomAuctionService iRoomAuctionService;
    @Autowired
    private IRoomWishService iRoomWishService;


    @Override
    public boolean giveGift(AppGiftGiveRequest request, Long userId) {
        Gift verify = iGiftService.verify(request.getGiftId());
        Room room = iRoomService.verify(request.getRoomId());
        if (room.getRoomTypeId() == 5 && !request.getAimUserId().contains(room.getUserId())) {
            RoomAuction auction = iRoomAuctionService.verify(request.getId());
            Assert.isTrue(!auction.getUserId().equals(userId), "当前房间被拍者不允许自己拍自己!");
        }

        if (this.giveGift(room.getId(), room.getRoomNumber(), verify, userId, request.getAimUserId())) {
            //异步发送到聊天室属性里面
            iMessageService.sendRoomRank(room.getGroupId(), room.getUserId().toString(), room.getId(), userId, verify.getGiftPrice());
            //房间心动拍拍
            if (!request.getAimUserId().isEmpty() && room.getRoomTypeId() == 5 && !request.getAimUserId().contains(room.getUserId())) {
                for (Long aLong : request.getAimUserId()) {
                    iMessageService.sendRoomRankAimUser(room.getGroupId(), room.getUserId().toString(), room.getId(), userId, verify.getGiftPrice(), aLong);
                }
            }
            //房间开启心愿单的话统计赠送数量及总共数量
            if (request.getAimUserId().contains(room.getUserId())) {
                iRoomWishService.isRoomWish(request.getGiftId(), room.getUserId(), room.getId());
            }
        }
        return true;
    }

    @Override
    public boolean giveGift(Long roomId, String roomNumber, Gift gift, Long userId, Set<Long> aimUserId) {
        String one = iParamService.selectConfigByKey("GIFT_RATIO");
        String giftMoney = iParamService.selectConfigByKey("GIFT_MONEY");
        String giftPrice = iParamService.selectConfigByKey("GIFT_PRICE");
        BigDecimal ratio = new BigDecimal(one);
        Integer contrNum = ratio.multiply(gift.getGiftPrice()).intValue();
        BigDecimal totalMoney = gift.getGiftPrice().multiply(BigDecimal.valueOf(aimUserId.size()));

        BigDecimal bigDecimal = gift.getGiftPrice().multiply(new BigDecimal(giftMoney)).multiply(new BigDecimal(giftPrice)).setScale(2, BigDecimal.ROUND_DOWN);

        AppUserBalanceOpetRequest opetRequest = new AppUserBalanceOpetRequest();
        opetRequest.setBalTypel(UserBalanceConstants.BalanceType.balance);
        opetRequest.setContent("赠送礼物:" + gift.getGiftName());
        opetRequest.setMoney(totalMoney);
        opetRequest.setType(UserBalanceConstants.Type.Zhi);
        opetRequest.setUserId(userId);
        R<Boolean> booleanR = remoteAppUserService.docBalance(opetRequest, SecurityConstants.APP);
        Assert.isTrue(booleanR.getCode() == 2000, booleanR.getmessage());


        List<AppUserBalanceAocRequest> requests = aimUserId.stream().map(res -> {
            AppUserBalanceAocRequest aocRequest = new AppUserBalanceAocRequest();
            aocRequest.setUserId(res);
            aocRequest.setMoney(bigDecimal);
            aocRequest.setRemark("来自用户id" + userId + "赠送礼物" + gift.getGiftName());
            return aocRequest;
        }).collect(Collectors.toList());


        /*请求内部接口增加提现金额*/
        if (!requests.isEmpty()) {
            AppUserBalanceBatchOpetRequest batchOpetRequest = new AppUserBalanceBatchOpetRequest();
            batchOpetRequest.setType(UserBalanceConstants.BalanceType.balance);
            batchOpetRequest.setBalTypel(UserBalanceConstants.BalanceType.Cash);
            batchOpetRequest.setRequests(requests);
            remoteAppUserService.aocBalanceByBatch(batchOpetRequest, SecurityConstants.APP);
        }

        iRoomGiftRecordService.addRecord(roomId, roomNumber,
                userId, aimUserId, gift.getId(),
                "来自用户id" + userId + "赠送" + gift.getGiftName(), gift.getGiftPrice(), contrNum);
        return true;
    }


}
